package net.oschina.dajiangnan.wx.process.pay.tools;

import net.oschina.dajiangnan.util.XStreamHelper;
import net.oschina.dajiangnan.wx.util.HttpTookit;
import net.oschina.dajiangnan.wx.util.RandomUtil;
import net.oschina.dajiangnan.wx.util.Signature;
import org.apache.log4j.Logger;

import net.oschina.dajiangnan.wx.constants.Config;
import net.oschina.dajiangnan.wx.process.pay.xmlbean.MicropayReq;
import net.oschina.dajiangnan.wx.process.pay.xmlbean.MicropayResp;


public class Micropay {

    private static Logger logger = Logger.getLogger(Micropay.class);
    
    private static final String url = "https://api.mch.weixin.qq.com/pay/micropay";
    
    
    
    
    
    
    
    /**
     * 提交刷卡支付API
     * https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1
     * 
     * 
     * @param device_info 终端设备号(商户自定义，如门店编号)
     * @param body 商品描述
     * @param detail 商品详情
     * @param out_trade_no 商户系统内部的订单号,32个字符内、可包含字母
     * @param total_fee 订单总金额，单位为分
     * @param spbill_create_ip 调用微信支付API的机器IP

     * 
     * @return
     */
    public static Result submitMicropay(String device_info,String body,String detail,String out_trade_no,int total_fee,String spbill_create_ip,String auth_code){
        MicropayReq micropayReq = new MicropayReq();
        micropayReq.setAppid(Config.appid);
        micropayReq.setMch_id(Config.mch_id);
        micropayReq.setDevice_info(device_info);
        micropayReq.setNonce_str(RandomUtil.getRandomStr(32));
        micropayReq.setBody(body);
        micropayReq.setDetail(detail);
        micropayReq.setOut_trade_no(out_trade_no);
        micropayReq.setTotal_fee(String.valueOf(total_fee));
        micropayReq.setSpbill_create_ip(spbill_create_ip);
        micropayReq.setAuth_code(auth_code);
        
        try {
            micropayReq.setSign(Signature.getSign(micropayReq, null, Config.apikey));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        
        logger.debug("提交刷卡支付请求参数如下\r\n" + micropayReq);
        String xmlStr = new HttpTookit().doPost(url,micropayReq.toString(),null,null,"UTF-8");
        MicropayResp micropayResp = XStreamHelper.toBean(xmlStr, MicropayResp.class);
        logger.debug("提交刷卡支付应答内容如下\r\n" + micropayResp);
        

        if(!micropayResp.islegalSign(Config.apikey)){
            Result result = new Result();
            result.setMsg("MicropayResp UnlegalSign");
            return result;
        }
        
        if(micropayResp.getReturn_code().equals("SUCCESS")){
            Result result = new Result();
            result.setMicropayResp(micropayResp);
            return result;
        }else{
            return null;
        }
    }
    
    
    public static class Result{
        private String msg;
        private MicropayResp micropayResp;
        
        public String getMsg() {
        
            return msg;
        }
        
        public void setMsg(String msg) {
        
            this.msg = msg;
        }

        
        public MicropayResp getMicropayResp() {
        
            return micropayResp;
        }

        
        public void setMicropayResp(MicropayResp micropayResp) {
        
            this.micropayResp = micropayResp;
        }

        
    }
    
}
